﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CharacterCreation.Web.Models {
    public static class LevelCalculations {
        /// <summary>
        /// 1 = 3
        /// 1 = 2
        /// 1 = 1
        /// 3 = 2
        /// 2 = 1
        /// </summary>
        public static int CalculateSkill(int statistic, int xp) {
            int i = 0;
            decimal total = statistic;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 3;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + 2;
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + (decimal)(2d / 3d);
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + (decimal)(1d / 2d);
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (decimal)(1d / 3d);
                    xp = xp - 1;
                }
                else if (total < statistic * 7) {
                    total = total + (decimal)(1d / 4d);
                    xp = xp - 1;
                }
                else {
                    total = total + (decimal)(1d / 5d);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }

        /// <summary>
        /// 1 = 2
        /// 1 = 1
        /// 3 = 2
        /// 2 = 1
        /// 3 = 1
        /// </summary>
        /// <param name="statistic"></param>
        /// <param name="xp"></param>
        /// <returns></returns>
        public static int CalculateSpecial(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 2;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + (decimal)(2d / 3d);
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + (decimal)(1d / 2d);
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (decimal)(1d / 3d);
                    xp = xp - 1;
                }
                else if (total < statistic * 7) {
                    total = total + (decimal)(1d / 4d);
                    xp = xp - 1;
                }
                else {
                    total = total + (decimal)(1d / 5d);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }

        /// <summary>
        /// 2 = 5
        /// 2 = 3
        /// 3 = 4
        /// </summary>
        /// <param name="statistic"></param>
        /// <param name="xp"></param>
        /// <returns></returns>
        public static int CalculateEnergy(int statistic, int beginvalue, int xp) {
            decimal total = beginvalue;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 2.5M;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + (decimal)(3d / 2d);
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + (decimal)(4d / 3d);
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (decimal)(3d / 4d);
                    xp = xp - 1;
                }
                else {
                    total = total + (decimal)(1d / 2d);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }

        public static int CalculateStatistic(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < 24) {
                    total = total + (1M / 3M);
                    xp = xp - 1;
                }
                else if (total < 30) {
                    total = total + (1M / 4M);
                    xp = xp - 1;
                }
                else if (total < 36) {
                    total = total + (1M / 5M);
                    xp = xp - 1;
                }
                else if (total < 42) {
                    total = total + (1M / 6M);
                    xp = xp - 1;
                }
                else {
                    total = total + (1M / 7M);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }

        public static int CalculateHpAndEssence(int statistic, double xp) {
            return statistic + (int)((xp / 3) * 2);
        }

        public static int CalculateSkillXPCost(int statistic, int targetLevel) {
            var xp = 0;
            decimal total = targetLevel;

            while (total > statistic) {
                if (total < statistic * 2) {
                    total = total - 3;
                    xp++;
                }
                else if (total < statistic * 3) {
                    total = total - 2;
                    xp++;
                }
                else if (total < statistic * 4) {
                    total = total - 1;
                    xp++;
                }
                else if (total < statistic * 5) {
                    total = total - (2M / 3M);
                    xp++;
                }
                else if (total < statistic * 6) {
                    total = total - (1M / 2M);
                    xp++;
                }
                else if (total < statistic * 7) {
                    total = total - (1M / 3M);
                    xp++;
                }
                else if (total < statistic * 8) {
                    total = total - (1M / 4M);
                    xp++;
                }
                else {
                    total = total - (1M / 5M);
                    xp++;
                }
            }

            return xp - 1;
        }

        public static int GetStatisticLevel(string statistics) {
            Character character = CharacterService.Instance.SelectedCharacter;
            if (character != null && statistics != null && !string.IsNullOrWhiteSpace(statistics)) {
                var total = 0;
                var number = 0;
                var array = statistics.Split(';');
                if (!string.IsNullOrWhiteSpace(array[0])) {
                    total += character.Strength.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[1])) {
                    total += character.Dexterity.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[2])) {
                    total += character.Fighting.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[3])) {
                    total += character.Constitution.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[4])) {
                    total += character.Perception.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[5])) {
                    total += character.Intelligence.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[6])) {
                    total += character.WillPower.TotalLevel;
                    number++;
                }
                if (!string.IsNullOrWhiteSpace(array[7])) {
                    total += character.Charisma.TotalLevel;
                    number++;
                }
                if (number > 0) {
                    return (int)(total / number);
                }
            }
            return 0;
        }
    }
}